package com.huoyun.car.action;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.huoyun.base.action.BaseAction;
import com.huoyun.base.bean.Page;
import com.huoyun.car.bean.Car;
import com.huoyun.car.bean.CarLicense;
import com.huoyun.car.bean.CarPubInfo;
import com.huoyun.car.bean.Driver;
import com.huoyun.car.model.CarDriverOwnerModel;
import com.huoyun.car.model.CarModel;
import com.huoyun.car.service.CarLicenseService;
import com.huoyun.car.service.CarPubInfoService;
import com.huoyun.car.service.CarService;
import com.huoyun.car.service.DriverService;
import com.huoyun.order.service.RateService;
import com.huoyun.order.service.evt.GetRateByCarIdEvt;
import com.huoyun.user.bean.User;
import com.huoyun.util.DateUtil;
import com.huoyun.util.FileUtil;
import com.huoyun.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;

public class CarAction extends BaseAction {

	/**
	 * 司机身份证
	 */
	private File driverIdentityCardFile;
	private String driverIdentityCardFileFileName;

	/**
	 * 司机驾驶证
	 */
	private File driverDrivingLicenseFile;
	private String driverDrivingLicenseFileFileName;

	/**
	 * 车辆运营证
	 */
	private File carDrivingLicenseFile;
	private String carDrivingLicenseFileFileName;
	private CarLicenseService carLicenseService;
	private CarService carService;
	private DriverService driverService;

	private Car car;
	private Driver driver;

	private List<CarModel> myRegistedCarsList;

	private List<Driver> driverList;
	private long driverId; // 当前车辆关联的司机id
	private String cardNumber1; // 车牌照-省（苏）
	private String cardNumber2; // 车牌照-市（A）
	private String cardNumber3; // 车牌号-详细（875P6）

	private Page ratePage;
	private RateService rateService;
	private GetRateByCarIdEvt evt;
	private List<String> rateItems;

	private long carPubInfoId;
	private long carId;
	private CarPubInfo carPubInfo;
	/**
	 * 管理"车源信息"的service
	 */
	private CarPubInfoService carPubInfoService;

	/**
	 * 车源信息展示
	 * http://localhost:8080/huoyun/car/biddingCarDetail.action?carPubInfoId=15
	 */
	public String getInfoForCargoOwner() {
		try {
			carPubInfo = carPubInfoService.getCarPubInfoById(carPubInfoId);
			CarDriverOwnerModel carDriverOwnerModel = carService.getCarDriverOwnerModelByCarId(carPubInfo.getCarId());
			System.out.println(carPubInfo.getCarId());
			car = carDriverOwnerModel.getCar();
			driver = carDriverOwnerModel.getDriver();
			// 评价信息
			evt = new GetRateByCarIdEvt();
			evt.setCarId(carPubInfo.getCarId());
			evt.setPageNo(pageNo);
			evt.setPageCount(PAGE_COUNT);
			ratePage = rateService.queryRate(evt);
			rateItems = rateService.queryAllRateItemByCarId(carPubInfo.getCarId());

		} catch (Exception e) {

		}

		return SUCCESS;
	}

	public List<String> getRateItems() {
		return rateItems;
	}

	public void setRateItems(List<String> rateItems) {
		this.rateItems = rateItems;
	}

	public long getCarId() {
		return carId;
	}

	public void setCarId(long carId) {
		this.carId = carId;
	}

	public String preCarGate() {
		return SUCCESS;
	}

	public String saveAll() {
//		driverService.saveDriver(driver);
		car.setUserId(getLoginUser().getId());
		// 拼接车牌号
		String cardNumber = cardNumber1.toUpperCase() + cardNumber2.toUpperCase() + cardNumber3.toUpperCase();
		car.setCardNumber(cardNumber);
		// 设置为“未审核”
		car.setStatus(-1);
		carService.saveCar(car);
		return SUCCESS;
	}

	public String addDriver() {
		driver.setUserId(getLoginUser().getId());
		driver.setStatus(-1);	//设置审核状态为"未审核"
		driverService.saveDriver(driver);
		driverList = driverService.getDriverByUserId(getLoginUser().getId());
		driverId = driver.getId();
		return SUCCESS;
	}

	public String preAddDriverQuick() {
		driverList = driverService.getDriverByUserId(getLoginUser().getId());
		return SUCCESS;
	}
	
	public String preAddDriver() {
		driverList = driverService.getDriverByUserId(getLoginUser().getId());
		return SUCCESS;
	}

	public String showMyRegistedDrivers() {
		driverList = driverService.getDriverByUserId(getLoginUser().getId());
		return SUCCESS;
	}

	public Page getRatePage() {
		return ratePage;
	}

	public void setRatePage(Page ratePage) {
		this.ratePage = ratePage;
	}

	public RateService getRateService() {
		return rateService;
	}

	public void setRateService(RateService rateService) {
		this.rateService = rateService;
	}

	public String preRegCar() {
		return SUCCESS;
	}

	public String selectRegCarMethod() {
		return SUCCESS;
	}

	public String preFastRegCar() {
		return SUCCESS;
	}

	public String fastRegCar() {
		String path = FileUtil.UPLOAD_LICENSE_DIR + "/" + DateUtil.getCurrentDate();

		String saveFolderPath = ServletActionContext.getRequest().getRealPath(path);

		File saveFolder = new File(saveFolderPath);

		if (!saveFolder.exists()) {
			saveFolder.mkdir();
		}

		File driverIdentityCardFile_new = new File(saveFolderPath, UUID.randomUUID().toString() + "." + StringUtil.getLastName(driverIdentityCardFileFileName));
		File driverDrivingLicenseFile_new = new File(saveFolderPath, UUID.randomUUID().toString() + "." + StringUtil.getLastName(driverDrivingLicenseFileFileName));
		File carDrivingLicenseFile_new = new File(saveFolderPath, UUID.randomUUID().toString() + "." + StringUtil.getLastName(carDrivingLicenseFileFileName));

		try {
			FileUtil.copyFile(driverIdentityCardFile, driverIdentityCardFile_new);
			FileUtil.copyFile(driverDrivingLicenseFile, driverDrivingLicenseFile_new);
			FileUtil.copyFile(carDrivingLicenseFile, carDrivingLicenseFile_new);
		} catch (Exception e) {
			this.setResultMsg("�ļ��ϴ�����");

			return INPUT;
		}

		CarLicense carLicense = new CarLicense();
		carLicense.setUserId(getLoginUser().getId());
		carLicense.setDriverIdentityCard(path + "/" + driverIdentityCardFile_new.getName());
		carLicense.setDriverDrivingLicense(path + "/" + driverDrivingLicenseFile_new.getName());
		carLicense.setCarDrivingLicense(path + "/" + carDrivingLicenseFile_new.getName());
		carLicense.setStatus(0);
		carLicense.setAddTime(new Date());

		carLicenseService.saveCarLicense(carLicense);

		return SUCCESS;
	}

	/**
	 * 获取当前用户的所有注册车辆信息
	 * @return SUCCESS
	 * @author ChengXin
	 */
	public String showMyRegistedCars() {

		myRegistedCarsList = carService.getCarModelListByUserId(getLoginUser().getId());

		return SUCCESS;
	}

	public String editRegistedCar() {
		int carId = Integer.parseInt(getRequest().getParameter("carId"));
		car = carService.getCarById(carId);
		driver = driverService.getDriverById(car.getDriverId());

		return SUCCESS;
	}

	public String editRegistedDriver() {
		int driverId = Integer.parseInt(getRequest().getParameter("driverId"));
		driver = driverService.getDriverById(driverId);
		return SUCCESS;
	}

	public String updateDriver() {
		int driverId = Integer.parseInt(getRequest().getParameter("driverId"));
		driver.setId(driverId);
		driver.setStatus(-1);	//设置审核状态为"未审核"
		driverService.updateAll(driver);
		return SUCCESS;
	}

	public String deleteDriver() {
		int driverId = Integer.parseInt(getRequest().getParameter("driverId"));
		driverService.delete(driverId);
		return SUCCESS;
	}

	public String updateRegistedCar() {
		int carId = Integer.parseInt(getRequest().getParameter("carId"));
		// Car carTmp = carService.getCarById(carId);
		car.setId(carId);
		String cardNumber = cardNumber1.toUpperCase() + cardNumber2.toUpperCase() + cardNumber3.toUpperCase();
		car.setCardNumber(cardNumber);
		// 设置为“未审核”
		car.setStatus(-1);
		// driver.setId(carTmp.getDriverId());
		carService.updateAll(car);
		// driverService.updateAll(driver);

		return SUCCESS;
	}

	public String deleteCar() {
		int carId = Integer.parseInt(getRequest().getParameter("carId"));
		carService.delete(carId);
		return SUCCESS;
	}

	public long getDriverId() {
		return driverId;
	}

	public void setDriverId(long driverId) {
		this.driverId = driverId;
	}

	public String getCardNumber1() {
		cardNumber1 = car.getCardNumber().substring(0, 1);
		return cardNumber1;
	}

	public void setCardNumber1(String cardNumber1) {
		this.cardNumber1 = cardNumber1;
	}

	public CarPubInfoService getCarPubInfoService() {
		return carPubInfoService;
	}

	public void setCarPubInfoService(CarPubInfoService carPubInfoService) {
		this.carPubInfoService = carPubInfoService;
	}

	public String getCardNumber2() {
		try {
			cardNumber2 = car.getCardNumber().substring(1, 2);
		} catch (IndexOutOfBoundsException ex) {
			return ERROR;
		}

		return cardNumber2;
	}

	public void setCardNumber2(String cardNumber2) {
		this.cardNumber2 = cardNumber2;
	}

	public String getCardNumber3() {
		try {
			cardNumber3 = car.getCardNumber().substring(2);
		} catch (Exception ex) {
			return ERROR;
		}

		return cardNumber3;
	}

	public void setCardNumber3(String cardNumber3) {
		this.cardNumber3 = cardNumber3;
	}

	public List<Driver> getDriverList() {
		driverList = driverService.getDriverByUserId(getLoginUser().getId());
		return driverList;
	}

	public void setDriverList(List<Driver> driverList) {
		this.driverList = driverList;
	}

	public Driver getDriver() {
		return driver;
	}

	public void setDriver(Driver driver) {
		this.driver = driver;
	}

	public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}

	public DriverService getDriverService() {
		return driverService;
	}

	public void setDriverService(DriverService driverService) {
		this.driverService = driverService;
	}

	public CarService getCarService() {
		return carService;
	}

	public void setCarService(CarService carService) {
		this.carService = carService;
	}

	public File getDriverIdentityCardFile() {
		return driverIdentityCardFile;
	}

	public void setDriverIdentityCardFile(File driverIdentityCardFile) {
		this.driverIdentityCardFile = driverIdentityCardFile;
	}

	public String getDriverIdentityCardFileFileName() {
		return driverIdentityCardFileFileName;
	}

	public void setDriverIdentityCardFileFileName(String driverIdentityCardFileFileName) {
		this.driverIdentityCardFileFileName = driverIdentityCardFileFileName;
	}

	public File getDriverDrivingLicenseFile() {
		return driverDrivingLicenseFile;
	}

	public void setDriverDrivingLicenseFile(File driverDrivingLicenseFile) {
		this.driverDrivingLicenseFile = driverDrivingLicenseFile;
	}

	public String getDriverDrivingLicenseFileFileName() {
		return driverDrivingLicenseFileFileName;
	}

	public void setDriverDrivingLicenseFileFileName(String driverDrivingLicenseFileFileName) {
		this.driverDrivingLicenseFileFileName = driverDrivingLicenseFileFileName;
	}

	public File getCarDrivingLicenseFile() {
		return carDrivingLicenseFile;
	}

	public void setCarDrivingLicenseFile(File carDrivingLicenseFile) {
		this.carDrivingLicenseFile = carDrivingLicenseFile;
	}

	public String getCarDrivingLicenseFileFileName() {
		return carDrivingLicenseFileFileName;
	}

	public void setCarDrivingLicenseFileFileName(String carDrivingLicenseFileFileName) {
		this.carDrivingLicenseFileFileName = carDrivingLicenseFileFileName;
	}

	public CarLicenseService getCarLicenseService() {
		return carLicenseService;
	}

	public void setCarLicenseService(CarLicenseService carLicenseService) {
		this.carLicenseService = carLicenseService;
	}


	public List<CarModel> getMyRegistedCarsList() {
		return myRegistedCarsList;
	}


	public void setMyRegistedCarsList(List<CarModel> myRegistedCarsList) {
		this.myRegistedCarsList = myRegistedCarsList;
	}
	
	
	public String checkDriverIdCardNumber() throws IOException
	{
		this.setResultMsg("false");
		String driverIdCardNumber = "";
		
		if(driver != null)
		{
			driverIdCardNumber = driver.getIdCardNumber();
		}
		
		
		if(StringUtils.isNotEmpty(driverIdCardNumber))
		{
			try{
				Driver driver = driverService.getDriverByIdCardNumber(getLoginUser().getId(), driverIdCardNumber);
			
				if(driver == null)
				{
					this.setResultMsg("true");
				}
			}catch (Exception e) {
				//log.error("查询用户失败！UserAction - [checkUser]: Exception catched:" + e.getMessage());
			}
		
		}
		
		HttpServletResponse response = (HttpServletResponse)ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
		
		try
		{
			response.getWriter().print(this.getResultMsg());
		}catch (IOException e) {
			//log.error("校验用户【"+ username +"】输出流发生错误！原因：" + e.fillInStackTrace());
			
			throw e;
		}
		return null;	
	}
	
	public String checkDriverDrivingLicenseNumber() throws IOException
	{
		this.setResultMsg("false");
		String driverDrivingLicenseNumber = "";
		
		if(driver != null)
		{
			driverDrivingLicenseNumber = driver.getDrivingLicenseNumber();
		}
		
		
		if(StringUtils.isNotEmpty(driverDrivingLicenseNumber))
		{
			try{
				Driver driver = driverService.getDriverByDrivingLicenseNumber(getLoginUser().getId(), driverDrivingLicenseNumber);
			
				if(driver == null)
				{
					this.setResultMsg("true");
				}
			}catch (Exception e) {
				//log.error("查询用户失败！UserAction - [checkUser]: Exception catched:" + e.getMessage());
			}
		
		}
		
		HttpServletResponse response = (HttpServletResponse)ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
		
		try
		{
			response.getWriter().print(this.getResultMsg());
		}catch (IOException e) {
			//log.error("校验用户【"+ username +"】输出流发生错误！原因：" + e.fillInStackTrace());
			
			throw e;
		}
		return null;	
	}
	
//	public String checkCarCardNumber() throws IOException
//	{
//		this.setResultMsg("false");
//		String carCardNumber = "";
//		
//		if(car != null)
//		{
//			carCardNumber = car.getCardNumber();
//		}
//		
//		
//		if(StringUtils.isNotEmpty(carCardNumber))
//		{
//			try{
//				Car car = carService.getCarByDrivingLicenseNumber(getLoginUser().getId(), driverDrivingLicenseNumber);
//			
//				if(driver == null)
//				{
//					this.setResultMsg("true");
//				}
//			}catch (Exception e) {
//				//log.error("查询用户失败！UserAction - [checkUser]: Exception catched:" + e.getMessage());
//			}
//		
//		}
//		
//		HttpServletResponse response = (HttpServletResponse)ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);
//		
//		try
//		{
//			response.getWriter().print(this.getResultMsg());
//		}catch (IOException e) {
//			//log.error("校验用户【"+ username +"】输出流发生错误！原因：" + e.fillInStackTrace());
//			
//			throw e;
//		}
//		return null;	
//	}
	

	public long getCarPubInfoId() {
		return carPubInfoId;
	}

	public void setCarPubInfoId(long carPubInfoId) {
		this.carPubInfoId = carPubInfoId;
	}

	public CarPubInfo getCarPubInfo() {
		return carPubInfo;
	}

	public void setCarPubInfo(CarPubInfo carPubInfo) {
		this.carPubInfo = carPubInfo;
	}
}
